//
//  FWIconTextButton.m
//  iOSToolkit
//
//  Created by Evan Xie on 5/26/15.
//  Copyright (c) 2015 Family Wings. All rights reserved.
//

#import "FWIconTextButton.h"


@interface FWIconTextButton ()
{
    UIImageView *_iconImageView;
    UILabel *_textLabel;
}

@property (nonatomic, assign) DirectionType directionType;
@property (nonatomic, assign) AlignmentType alignmentType;
@property (nonatomic, assign) PositionType iconTextPositionType;

@property (nonatomic, assign) CGFloat horinzontalSpacing;
@property (nonatomic, assign) CGFloat verticalSpacing;

@end

@implementation FWIconTextButton

+ (instancetype)buttonWithIcon:(UIImage *)iconImage andText:(NSString *)text frame:(CGRect)frame
{
    return [[[self class] alloc] initWithIcon:iconImage andText:text frame:frame];
}

- (instancetype)init
{
    return [self initWithIcon:nil andText:nil frame:CGRectZero];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    return [self initWithIcon:nil andText:nil frame:frame];
}

- (instancetype)initWithIcon:(UIImage *)iconImage andText:(NSString *)text frame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupViewsWithIconImage:iconImage andText:text];
    }
    return self;
}

- (void)awakeFromNib
{
    [self setupViewsWithIconImage:nil andText:nil];
}

- (void)setupViewsWithIconImage:(UIImage *)iconImage andText:(NSString *)text
{
    self.cornerRadius = 5.0;

    _horinzontalSpacing = 10.0;
    _verticalSpacing = 6.0;
    
    _iconImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
    _iconImageView.contentMode = UIViewContentModeCenter;
    
    _textLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    _textLabel.textColor = [UIColor darkTextColor];
    _textLabel.backgroundColor = [UIColor clearColor];
    _textLabel.font = [UIFont systemFontOfSize:16.0];
    _textLabel.adjustsFontSizeToFitWidth = YES;
    
    [self addSubview:_iconImageView];
    [self addSubview:_textLabel];
    
    _iconImageView.image = iconImage;
    _textLabel.text = text;
    
    [self setDirectionType:DirectionTypeHorinzontal alignmentType:AlignmentTypeCenter andPositionType:PositionTypeIconFirstTextLast];
}

- (void)setIconImage:(UIImage *)iconImage andText:(NSString *)text
{
    if (iconImage != _iconImageView.image || ![text isEqualToString:_textLabel.text]) {
        _iconImageView.image = iconImage;
        _textLabel.text = text;
        [self setNeedsLayout];
    }
}

- (void)setTextColor:(UIColor *)textColor textFont:(UIFont *)textFont
{
    _textLabel.textColor = textColor;
    
    if (!textFont) {
        return;
    }
    
    if (_textLabel.font != textFont) {
        _textLabel.font = textFont;
        [self setNeedsLayout];
    }
}

- (void)setDirectionType:(DirectionType)directionType alignmentType:(AlignmentType)alignmentType andPositionType:(PositionType)iconTextPositionType
{
    if (directionType != _directionType || alignmentType != _alignmentType || iconTextPositionType != _iconTextPositionType) {
        _directionType = directionType;
        _alignmentType = alignmentType;
        _iconTextPositionType = iconTextPositionType;
        
        switch (self.directionType) {
            case DirectionTypeHorinzontal:
                
                if (self.alignmentType == AlignmentTypeCenter) {
                    
                    if (self.iconTextPositionType == PositionTypeIconFirstTextLast) {
                        _textLabel.textAlignment = NSTextAlignmentLeft;
                    } else {
                        _textLabel.textAlignment = NSTextAlignmentRight;
                    }
                    
                } else {
                    
                    // AlignmentTypeEnds
                    if (self.iconTextPositionType == PositionTypeIconFirstTextLast) {
                        _textLabel.textAlignment = NSTextAlignmentRight;
                    } else {
                        _textLabel.textAlignment = NSTextAlignmentLeft;
                    }
                }
                
                break;
                
            case DirectionTypeVertical:
                
                _textLabel.textAlignment = NSTextAlignmentCenter;
                break;
        }
        
        [self setNeedsLayout];
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat buttonWidth = self.frame.size.width;
    CGFloat buttonHeight = self.frame.size.height;
    
    CGSize imageSize = _iconImageView.image.size;
    CGSize textSize = CGSizeZero;
    
    if (_textLabel.text.length > 0) {
        CGFloat availableWidth = 0;
        CGFloat availableHeight = 0;
        
        if (self.directionType == DirectionTypeHorinzontal) {
            availableWidth = buttonWidth - self.contentEdgeInsets.left - self.contentEdgeInsets.right - imageSize.width;
            availableHeight = buttonHeight - self.contentEdgeInsets.top - self.contentEdgeInsets.bottom;
            if (imageSize.width > 0) {
                availableWidth -= self.horinzontalSpacing;
            }
        } else {
            // Vertical
            availableWidth = buttonWidth - self.contentEdgeInsets.left - self.contentEdgeInsets.right;
            availableHeight = buttonHeight - self.contentEdgeInsets.top - self.contentEdgeInsets.bottom - imageSize.height;
            if (imageSize.height > 0) {
                availableHeight -= self.verticalSpacing;
            }
        }
        
        textSize = [_textLabel.text boundingRectWithSize:CGSizeMake(availableWidth, availableHeight)
                                                 options:NSStringDrawingTruncatesLastVisibleLine
                                              attributes:@{NSFontAttributeName: _textLabel.font}
                                                 context:nil].size;
    }
    
    if (imageSize.width == 0) {
        CGFloat x = (buttonWidth - textSize.width) / 2.0;
        CGFloat y = (buttonHeight - textSize.height) / 2.0;
        _iconImageView.frame = CGRectZero;
        _textLabel.frame = CGRectMake(x, y, textSize.width, textSize.height);
        return;
    }
    
    if (textSize.width == 0) {
        CGFloat x = (buttonWidth - imageSize.width) / 2.0;
        CGFloat y = (buttonHeight - imageSize.height) / 2.0;
        _iconImageView.frame = CGRectMake(x, y, imageSize.width, imageSize.height);
        _textLabel.frame = CGRectZero;
        return;
    }
    
    CGFloat xOffsetIcon = 0.0;
    CGFloat yOffsetIcon = 0.0;
    CGFloat xOffsetText = 0.0;
    CGFloat yOffsetText = 0.0;
    
    switch (self.directionType) {
        case DirectionTypeHorinzontal:
            
            if (self.alignmentType == AlignmentTypeCenter) {
                
                if (self.iconTextPositionType == PositionTypeIconFirstTextLast) {
                    xOffsetIcon = (buttonWidth - textSize.width - imageSize.width - self.horinzontalSpacing) / 2.0;
                    xOffsetText = xOffsetIcon + imageSize.width + self.horinzontalSpacing;
                } else {
                    // PositionTypeTextFirstIconLast
                    xOffsetText = (buttonWidth - textSize.width - imageSize.width - self.horinzontalSpacing) / 2.0;
                    xOffsetIcon = xOffsetText + textSize.width + self.horinzontalSpacing;
                }
                
            } else {
                
                // AlignmentTypeEnds
                if (self.iconTextPositionType == PositionTypeIconFirstTextLast) {
                    xOffsetIcon = self.contentEdgeInsets.left;
                    xOffsetText = buttonWidth - self.contentEdgeInsets.right - textSize.width;
                } else {
                    // PositionTypeTextFirstIconLast
                    xOffsetText = self.contentEdgeInsets.left;
                    xOffsetIcon = buttonWidth - self.contentEdgeInsets.right - imageSize.width;
                }
            }
            
            yOffsetIcon = (buttonHeight - imageSize.height) / 2.0;
            yOffsetText = (buttonHeight - textSize.height) / 2.0;
            
            break;
            
        case DirectionTypeVertical:
            
            if (self.alignmentType == AlignmentTypeCenter) {
                
                if (self.iconTextPositionType == PositionTypeIconFirstTextLast) {
                    yOffsetIcon = (buttonHeight - textSize.height - imageSize.height - self.verticalSpacing) / 2.0;
                    yOffsetText = yOffsetIcon + imageSize.height + self.verticalSpacing;
                } else {
                    // PositionTypeTextFirstIconLast
                    yOffsetText = (buttonHeight - textSize.height - imageSize.height - self.verticalSpacing) / 2.0;
                    yOffsetIcon = yOffsetText + textSize.height + self.verticalSpacing;
                }
                
            } else {
                
                // AlignmentTypeEnds
                if (self.iconTextPositionType == PositionTypeIconFirstTextLast) {
                    yOffsetIcon = self.contentEdgeInsets.top;
                    yOffsetText = buttonHeight - self.contentEdgeInsets.bottom - textSize.height;
                } else {
                    // PositionTypeTextFirstIconLast
                    yOffsetText = self.contentEdgeInsets.top;
                    yOffsetIcon = buttonHeight - self.contentEdgeInsets.bottom - imageSize.height;
                }
            }
            
            xOffsetIcon = (buttonWidth - imageSize.width) / 2.0;
            xOffsetText = (buttonWidth - textSize.width) / 2.0;
            
            break;
    }
    
    _iconImageView.frame = CGRectMake(xOffsetIcon, yOffsetIcon, imageSize.width, imageSize.height);
    _textLabel.frame = CGRectMake(xOffsetText, yOffsetText, textSize.width, textSize.height);
}

@end
